package com.example.lenovo.weiyi;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.com.ruanmeng.demon.CommonM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * Created by Administrator on 2017/5/5.
 */

public class ChangeWeightActivity extends BaseActivity {
    @BindView(R.id.et_user_weight)
    EditText etUserWeight;
    @BindView(R.id.bt_queding)
    Button btQueding;
    @BindView(R.id.iv_weightdel)
    ImageView ivWeightdel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changeweight);
        ButterKnife.bind(this);
        ChangLayTitle("体重");
        init();
    }

    private void init() {
        btQueding.setClickable(false);
        etUserWeight.addTextChangedListener(this);
        etUserWeight.setText(getIntent().getStringExtra("tz"));
        etUserWeight.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        etUserWeight.setText(s);
                        etUserWeight.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    etUserWeight.setText(s);
                    etUserWeight.setSelection(2);
                }

                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        etUserWeight.setText(s.subSequence(0, 1));
                        etUserWeight.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }
        });

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        super.onTextChanged(charSequence, i, i1, i2);
        if (CommonUtil.getEditText(etUserWeight).isEmpty()) {
            btQueding.setBackgroundResource(R.drawable.ed_fullgeray);
            btQueding.setClickable(false);
            ivWeightdel.setVisibility(View.GONE);
        } else {
            btQueding.setBackgroundResource(R.drawable.ed_fullmain);
            btQueding.setClickable(true);
            ivWeightdel.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.bt_queding,R.id.iv_weightdel})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_weightdel:
                etUserWeight.setText("");
                break;
            case R.id.bt_queding:
                if (etUserWeight.getText().toString().endsWith(".")) {
                    CommonUtil.showToask(ChangeWeightActivity.this, "请输入正确的的体重！");
                    return;
                }
                if (etUserWeight.getText().toString().startsWith("0")) {
                    CommonUtil.showToask(ChangeWeightActivity.this, "请输入正确的的体重！");
                    return;
                }
                if (etUserWeight.getText().toString().endsWith(".")) {
                    CommonUtil.showToask(ChangeWeightActivity.this, "请输入正确的的体重！");
                    return;
                }
                //年龄在18-80之间，身高在130-220之间，体重在35-200kg之间
                if (Double.parseDouble(etUserWeight.getText().toString()) < 35 || Double.parseDouble(etUserWeight.getText().toString()) > 200) {
                    CommonUtil.showToask(ChangeWeightActivity.this, "请输入正确的体重！");
                    return;
                }
                getChangeData();
                break;
        }

    }

    /**
     * 修改个人资料
     */
    private void getChangeData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.UpdatePerMess, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(ChangeWeightActivity.this, "uid"));
        mRequest.add("weight", etUserWeight.getText().toString());
        CallServer.getRequestInstance().add(ChangeWeightActivity.this, 0, mRequest,
                new CustomHttpListener(ChangeWeightActivity.this, true, CommonM.class) {

                    @Override
                    public void doWork(Object data, boolean isOne) {
                        CommonM model = ((CommonM) data);
                        PreferencesUtils.putString(ChangeWeightActivity.this, "weight", etUserWeight.getText().toString());
                        finish();

                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        if (!("100").equals(obj.getString("msgcode"))) {
                            CommonUtil.showToask(ChangeWeightActivity.this, obj.getString("msg"));
                        }
                    }
                }, true, true);
    }
}
